package com.ml.carManager.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.appcompat.widget.AppCompatTextView;

import com.ml.carManager.R;
import com.ml.carManager.ui.dialog.listener.OnCancelClickListener;
import com.ml.carManager.ui.dialog.listener.OnEnterClickListener;


public class HintDialog extends Dialog implements View.OnClickListener {

    private TextView tvTittle;
    private TextView tvContent;

    private AppCompatTextView btnEnter, btnCancel;

    private View lineView;

    private String tittleString = "";
    private String contentString = "";
    private String enterString = "确定";
    private String cancelString = "取消";
    private int gravity = Gravity.CENTER_HORIZONTAL;
    /**
     * 确定按钮的文本颜色,默认为主题蓝色
     */
    private int enterTxtColor = 0xff497ebe;
    /**
     * 取消按钮的文本颜色,默认为二级文字灰
     */
    private int cancelTxtColor = 0xff888888;

    private boolean isShow = false;
    private boolean isShowCancelBtn = true;

    public HintDialog(Context context) {
        super(context, R.style.normal_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
        setContentView(R.layout.dialog_hint);
        initWidget();
        init();
    }

    private void initWidget() {
        tvTittle = (TextView) findViewById(R.id.dialog_hint_tittle);
        tvContent = (TextView) findViewById(R.id.dialog_hint_content);
        btnEnter = (AppCompatTextView) findViewById(R.id.dialog_hint_enter);
        btnCancel = (AppCompatTextView) findViewById(R.id.dialog_hint_cancel);
        lineView = findViewById(R.id.dialog_hint_line);

        setCanceledOnTouchOutside(true);
        btnEnter.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
            }
        });
    }

    private void init() {
    }

    /**
     * 设置取消Btn显示/隐藏
     *
     * @param isShowCancelBtn
     */
    public void setIsShowCancelBtn(boolean isShowCancelBtn) {
        this.isShowCancelBtn = isShowCancelBtn;
    }

    /**
     * 设置标题,如果为空的话则会隐藏
     *
     * @param tittleString
     */
    public void setTitleString(String tittleString) {
        this.tittleString = tittleString;
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        setTitleString(title.toString());
    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        setTitleString(getContext().getString(titleId));
    }

    /**
     * 设置提示语的文本
     *
     * @param contentString
     */
    public void setContentString(String contentString) {
        this.contentString = contentString;
    }

    /**
     * 设置确定按钮的文本
     */
    public void setEnterTxt(String str) {
        this.enterString = str;
    }

    /**
     * 设置取消按钮的文本
     */
    public void setCancelTxt(String str) {
        this.cancelString = str;
    }

    /**
     * 设置确定按钮的文本
     */
    public void setEnterTxtColor(@ColorInt int color) {
        this.enterTxtColor = color;
    }

    /**
     * 设置取消按钮的文本
     */
    public void setCancelTxtColor(@ColorInt int color) {
        this.cancelTxtColor = color;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    @Override
    public void show() {
        if (!isShow) {
            try {
                super.show();
            } catch (Exception e) {

            }
            tvContent.setGravity(gravity);
            if (!isShowCancelBtn) {
                lineView.setVisibility(View.GONE);
                btnCancel.setVisibility(View.GONE);
            }
            btnEnter.setTextColor(enterTxtColor);
            btnCancel.setTextColor(cancelTxtColor);
            if (!"".equals(tittleString.trim())) {
                tvTittle.setText(tittleString);
                tvTittle.setVisibility(View.VISIBLE);
            } else {
                tvTittle.setVisibility(View.GONE);
            }
            if (!"".equals(contentString.trim())) {
                tvContent.setText(contentString);
                tvContent.setVisibility(View.VISIBLE);
            } else {
                tvContent.setVisibility(View.GONE);
            }
            btnEnter.setText(enterString);
            btnCancel.setText(cancelString);
            isShow = true;
        }
    }

    private OnEnterClickListener onEnterClickListener = null;
    private OnCancelClickListener onCancelClickListener = null;

    public void setOnEnterClickListener(
            OnEnterClickListener onEnterClickListener) {
        this.onEnterClickListener = onEnterClickListener;
    }

    public void setOnCancelClickListener(
            OnCancelClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == btnEnter.getId()) {
            dismiss();
            if (onEnterClickListener != null) {
                onEnterClickListener.onEnter();
            }
        } else if (v.getId() == btnCancel.getId()) {
            dismiss();
            if (onCancelClickListener != null) {
                onCancelClickListener.onCancel();
            }
        }
    }
}
